package main

import (
	"encoding/json"
	"fmt"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"hello/controllers"
	"math/rand"
	"time"
)
type DeviceData struct {
	DeviceType int `json:"type"`
	Temp float32 `json:"temp"`
	TempSet float32 `json:"tempset"`
	Speed uint `json:"speed"`
	SpeedSet uint `json:"speedset"`
	HourSet uint8 `json:"Hset"`
	MinSet uint8 `json:"Mset"`
	Hour uint8 `json:"Hour"`
	Min uint8 `json:"Min"`
	TempMin float32 `json:"tempmin"`
	TempMax float32 `json:"tempmax"`
	SpeedMin uint `json:"spmin"`
	SpeedMax uint `json:"spmax"`
	ErrorInfo uint16 `json:"errinfo"`
	State uint16 `json:"state"`
	Mode uint8 `json:"mode"`
	Memory uint8 `json:"memory"`
}
type Alarm struct {
	InnerSensorErr              uint `json:"inner_sensor_err"`
	InnerSensorTempAlarm        uint `json:"inner_sensor_temp_alarm"`
	ExternalSensorErr           uint `json:"external_sensor_err"`
	ExternalSensorOverTemp      uint `json:"external_sensor_over_temp"`
	MechanicalLimitTempOverTemp uint `json:"mechanical_limit_temp_over_temp"`
	MechanicalLimitSensorErr    uint `json:"mechanical_limit_sensor_err"`
	MotorStalling               uint `json:"motor_stalling"`
	ExternalSensorDrop          uint `json:"external_sensor_drop"`
	EndOfTime                   uint `json:"end_of_time"`
	ExternalSensorNotPlugged    uint `json:"external_sensor_not_plugged"`
}

func onMsgHandler(client mqtt.Client, message mqtt.Message) {
	fmt.Println(string(message.Payload()))
}

func main(){
	config := mqtt.ClientOptions{}
	config.AddBroker("tcp://www.dcharles.top:1883")
	config.SetClientID("device_data")
	//config.SetOnConnectHandler(onHandler)
	config.SetDefaultPublishHandler(onMsgHandler)
	mqttclient := mqtt.NewClient(&config)
	if token := mqttclient.Connect();token.Wait() && token.Error() != nil {
		fmt.Println(token.Error())
	}
	topic1 := "GATEWAY/data"
	//topic2 := "GATEWAY/band"
	for {
		//msg := &DeviceData{
		//	DeviceType: rand.Intn(2),
		//	Temp:       float32(rand.Intn(500)) + 2.3,
		//	TempSet:    35.5,
		//	Speed:      500,
		//	SpeedSet:   550,
		//	HourSet:    1,
		//	MinSet:     12,
		//	Hour:       1,
		//	Min:        2,
		//	TempMin:    20.1,
		//	TempMax:    200.5,
		//	SpeedMin:   100,
		//	SpeedMax:   1000,
		//	ErrorInfo:  0b1000101001000000,
		//	State:      1,
		//	Mode:       1,
		//	Memory:     1,
		//}
		msg := controllers.CLJBHistoryData{
			GatewayID: "GATEWAY",
			Data:      controllers.CLJBDisplay{
					MachineType: rand.Intn(2),
					DisplayTemp:       float32(rand.Intn(500)) + 2.3,
					SettingTemp:    35.5,
					DisplaySpeed:      500,
					SettingSpeed:   550,
					SettingTimeHour:    1,
					SettingTimeMinute:     12,
					DisplayTimeHour:       1,
					DisplayTimeMinute:        2,
					TempRangeMin:    20.1,
					TempRangeMax:    200.5,
					SpeedMin:   100,
					SpeedMax:   1000,
					ErrorInfo:  0b1000101001000000,
					RunningStatus:      1,
					CurrentMode:       1,
					PowerLostMemory:     1,
			},
		}
		msgbyte, _ := json.Marshal(msg)
		token := mqttclient.Publish(topic1, 0, false, msgbyte)
		fmt.Println(token.Wait())
		token.Wait()
		time.Sleep(time.Second)
	}
}
